package com.yiren.dbaa.module.practice_set.map.view.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.yiren.dbaa.R;
import com.yiren.dbaa.currency.model.MultiLaneModel;
import com.yiren.dbaa.util.DensityUtil;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.List;


/**
 * Describe 车道采集子集的适配器
 */
public class MultiLaneAdapter extends BaseAdapter {

    private RadioGroup rgLineSelect;
    private RadioButton rbFullLine;
    private RadioButton rbDashLine;
    private RadioButton rbCenterLine;
    private LinearLayout llCenterLine;
    private RadioGroup rgCenterType;
    private RadioButton rbTwoShi;
    private RadioButton rbTwoXu;
    private RadioButton rbXuShi;
    private RadioGroup rgLineColor;
    private RadioButton rbWhite;
    private RadioButton rbYellow;
    private TextView tvAddWidth;
    private TextView tvLineWidth;
    private TextView tvReduceWidth;
    private LinearLayout llLineDis;
    private TextView tvAddDis;
    private TextView tvLineDis;
    private TextView tvReduceDis;
    private TextView tvCancel;
    private TextView tvSure;

    private AlertDialog dialog; //属性设置的弹出框

    private Context context;
    private List<MultiLaneModel> laneLists;
    private OnLaneClickListener listener;

    public MultiLaneAdapter(Context context, List<MultiLaneModel> laneLists, OnLaneClickListener listener) {
        this.context = context;
        this.laneLists = laneLists;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return laneLists.size();
    }

    @Override
    public Object getItem(int position) {
        return laneLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder mHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_multi_lane_child, parent, false);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        final MultiLaneModel model = laneLists.get(position);
        if (model.isShowNumber()) {
            mHolder.tvLaneNumber.setVisibility(View.VISIBLE);
            mHolder.tvLaneNumber.setText("线" + model.getNumber());
        } else {
            mHolder.tvLaneNumber.setVisibility(View.INVISIBLE);
        }
        String lineStyle = "";
        switch (model.getLineType()) {
            case 1:
                lineStyle += "实线";
                break;
            case 2:
                lineStyle += "虚线";
                break;
            case 3:
                lineStyle += "双实线";
                break;
            case 4:
                lineStyle += "双虚线";
                break;
            case 5:
                lineStyle += "虚实线";
                break;
        }
        switch (model.getLineColor()) {
            case 1:
                lineStyle += "；白色；";
                break;
            case 2:
                lineStyle += "；黄色；";
                break;
        }
        lineStyle += model.getLineWidth();
        if (model.getLineType() >= 3) {
            lineStyle += "；" + model.getLineDis();
        }
        mHolder.tvLaneStyle.setText(lineStyle);
        mHolder.tvAddChild.setVisibility(model.isAdd() ? View.VISIBLE : View.GONE);
        mHolder.tvDeleteChild.setVisibility(model.isDelete() ? View.VISIBLE : View.GONE);
        mHolder.tvLaneStyle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showStyleSetDialog(position);
            }
        });
        mHolder.tvAddChild.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onAddChild(position);
            }
        });
        mHolder.tvDeleteChild.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onDeleteChild(position);
            }
        });
        if (model.getTrajectoryPoints().size() == 0) {
            mHolder.tvStartSign.setVisibility(View.VISIBLE);
            mHolder.tvLanePoint.setText("起点");
            mHolder.tvLanePoint.setTextColor(context.getResources().getColor(R.color.main_gray));
            mHolder.tvLaneEnd.setText("终点");
            mHolder.tvLaneEnd.setTextColor(context.getResources().getColor(R.color.main_gray));
        } else {
            mHolder.tvStartSign.setVisibility(View.GONE);
            if (model.isCollectEnd()) {
                mHolder.tvLanePoint.setText("采点" + (model.getTrajectoryPoints().size() - 1));
                mHolder.tvLanePoint.setTextColor(context.getResources().getColor(R.color.main_gray));
                mHolder.tvLaneEnd.setText("终点");
                mHolder.tvLaneEnd.setTextColor(context.getResources().getColor(R.color.main_gray));
            } else {
                mHolder.tvLanePoint.setText("采点" + model.getTrajectoryPoints().size());
                mHolder.tvLanePoint.setTextColor(context.getResources().getColor(R.color.main_blue));
                mHolder.tvLaneEnd.setText("终点");
                mHolder.tvLaneEnd.setTextColor(context.getResources().getColor(R.color.main_blue));
            }
        }
        mHolder.tvLanePoint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (model.isCollectEnd()) {
                    MessageBox.show(context, "当前线段已经采集完成！");
                } else {
                    listener.onCollectPoint(position);
                }
            }
        });
        mHolder.tvLaneEnd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (model.isCollectEnd()) {
                    MessageBox.show(context, "当前线段已经采集完成！");
                } else {
                    listener.onCollectEnd(position);
                }
            }
        });
        return convertView;
    }

    /**
     * 显示属性设置的弹出框
     */
    private void showStyleSetDialog(final int position) {
        dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        // 高度占了全屏，临时给了一个高度，代码未优化
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = (DensityUtil.getScreenWidth(context) * 6) / 10;
//        params.height = (screen.getHeight() * 6) / 10;
        dialog.getWindow().setAttributes(params);
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_lane_style_set);
        initWindowDialog(window);
        setWindowDialog(position);
        //线型
        rgLineSelect.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_full_line:  //实线
                    case R.id.rb_dash_line:
                        llCenterLine.setVisibility(View.GONE);
                        llLineDis.setVisibility(View.GONE);
                        break;
                    case R.id.rb_center_line:  //道路中心线
                        llCenterLine.setVisibility(View.VISIBLE);
                        llLineDis.setVisibility(View.VISIBLE);
                        break;
                }
            }
        });
        rgCenterType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_two_shi: //双实线
                        rbTwoShi.setChecked(true);
                        break;
                    case R.id.rb_two_xu:  //双虚线
                        rbTwoXu.setChecked(true);
                        break;
                    case R.id.rb_xu_shi:  //虚实线
                        rbXuShi.setChecked(true);
                        break;
                }
            }
        });
        //增加线宽
        tvAddWidth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int width = Integer.parseInt(tvLineWidth.getText().toString().trim());
                width++;
                tvLineWidth.setText("" + width);
            }
        });
        //减小线宽
        tvReduceWidth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int width = Integer.parseInt(tvLineWidth.getText().toString().trim());
                if (width > 5) {
                    width--;
                    tvLineWidth.setText("" + width);
                }
            }
        });
        //增加双黄线间距
        tvAddDis.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int width = Integer.parseInt(tvLineDis.getText().toString().trim());
//                if (width < 50) {
                width++;
                tvLineDis.setText("" + width);
//                }
            }
        });
        //减小双黄线间距
        tvReduceDis.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int width = Integer.parseInt(tvLineDis.getText().toString().trim());
                if (width > 5) {
                    width--;
                    tvLineDis.setText("" + width);
                }
            }
        });
        //取消
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissLineSetDialog();
            }
        });
        //确定
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (rgLineSelect.getCheckedRadioButtonId()) {
                    case R.id.rb_full_line:  //实线
                        laneLists.get(position).setLineType(1);
                        break;
                    case R.id.rb_dash_line:  //虚线
                        laneLists.get(position).setLineType(2);
                        break;
                    case R.id.rb_center_line:  //道路中心线
                        switch (rgCenterType.getCheckedRadioButtonId()) {
                            case R.id.rb_two_shi:  //双实线
                                laneLists.get(position).setLineType(3);
                                break;
                            case R.id.rb_two_xu:   //双虚线
                                laneLists.get(position).setLineType(4);
                                break;
                            case R.id.rb_xu_shi:   //虚实线
                                laneLists.get(position).setLineType(5);
                                break;
                        }
                        break;
                }
                switch (rgLineColor.getCheckedRadioButtonId()) {
                    case R.id.rb_white:  //白色
                        laneLists.get(position).setLineColor(1);
                        break;
                    case R.id.rb_yellow:  //黄色
                        laneLists.get(position).setLineColor(2);
                        break;
                }
                laneLists.get(position).setLineWidth(Integer.parseInt(tvLineWidth.getText().toString().trim()));
                if (llLineDis.getVisibility() == View.VISIBLE) {
                    laneLists.get(position).setLineDis(Integer.parseInt(tvLineDis.getText().toString().trim()));
                }
                listener.onStyleChange(position);
                notifyDataSetChanged();
                dismissLineSetDialog();
            }
        });
    }

    /**
     * 初始化弹出框控件
     *
     * @param window
     */
    private void initWindowDialog(Window window) {
        rgLineSelect = (RadioGroup) window.findViewById(R.id.rg_line_select);
        rbFullLine = (RadioButton) window.findViewById(R.id.rb_full_line);
        rbDashLine = (RadioButton) window.findViewById(R.id.rb_dash_line);
        rbCenterLine = (RadioButton) window.findViewById(R.id.rb_center_line);
        llCenterLine = (LinearLayout) window.findViewById(R.id.ll_center_type);
        rgCenterType = (RadioGroup) window.findViewById(R.id.rg_center_type);
        rbTwoShi = (RadioButton) window.findViewById(R.id.rb_two_shi);
        rbTwoXu = (RadioButton) window.findViewById(R.id.rb_two_xu);
        rbXuShi = (RadioButton) window.findViewById(R.id.rb_xu_shi);
        rgLineColor = (RadioGroup) window.findViewById(R.id.rg_line_color);
        rbWhite = (RadioButton) window.findViewById(R.id.rb_white);
        rbYellow = (RadioButton) window.findViewById(R.id.rb_yellow);
        tvAddWidth = (TextView) window.findViewById(R.id.tv_add_width);
        tvLineWidth = (TextView) window.findViewById(R.id.tv_line_width);
        tvReduceWidth = (TextView) window.findViewById(R.id.tv_reduce_width);
        llLineDis = (LinearLayout) window.findViewById(R.id.ll_line_dis);
        tvAddDis = (TextView) window.findViewById(R.id.tv_add_dis);
        tvLineDis = (TextView) window.findViewById(R.id.tv_line_dis);
        tvReduceDis = (TextView) window.findViewById(R.id.tv_reduce_dis);
        tvCancel = (TextView) window.findViewById(R.id.tv_cancel);
        tvSure = (TextView) window.findViewById(R.id.tv_sure);
    }

    /**
     * 设置弹出框的初始值
     */
    private void setWindowDialog(int position) {
        MultiLaneModel model = laneLists.get(position);
        switch (model.getLineType()) {
            case 1:
                rbFullLine.setChecked(true);
                llCenterLine.setVisibility(View.GONE);
                llLineDis.setVisibility(View.GONE);
                break;
            case 2:
                rbDashLine.setChecked(true);
                llCenterLine.setVisibility(View.GONE);
                llLineDis.setVisibility(View.GONE);
                break;
            case 3:
                rbCenterLine.setChecked(true);
                llCenterLine.setVisibility(View.VISIBLE);
                rbTwoShi.setChecked(true);
                llLineDis.setVisibility(View.VISIBLE);
                break;
            case 4:
                rbCenterLine.setChecked(true);
                llCenterLine.setVisibility(View.VISIBLE);
                rbTwoXu.setChecked(true);
                llLineDis.setVisibility(View.VISIBLE);
                break;
            case 5:
                rbCenterLine.setChecked(true);
                llCenterLine.setVisibility(View.VISIBLE);
                rbXuShi.setChecked(true);
                llLineDis.setVisibility(View.VISIBLE);
                break;
        }
        switch (model.getLineColor()) {
            case 1:
                rbWhite.setChecked(true);
                break;
            case 2:
                rbYellow.setChecked(true);
                break;
        }
        tvLineWidth.setText(model.getLineWidth() + "");
        if (model.getLineType() >= 3) {
            tvLineDis.setText(model.getLineDis() + "");
        }
    }

    /**
     * 关闭弹出框
     */
    protected void dismissLineSetDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    class ViewHolder {
        TextView tvLaneNumber;   //线编号
        TextView tvLaneStyle;    //线属性
        TextView tvLanePoint;    //采集点
        TextView tvStartSign;    //开始点标记
        TextView tvLaneEnd;      //结束点
        TextView tvAddChild;     //添加子项
        TextView tvDeleteChild;  //删除子项

        public ViewHolder(View itemView) {
            tvLaneNumber = (TextView) itemView.findViewById(R.id.tv_lane_number);
            tvLaneStyle = (TextView) itemView.findViewById(R.id.tv_lane_style);
            tvLanePoint = (TextView) itemView.findViewById(R.id.tv_lane_point);
            tvStartSign = (TextView) itemView.findViewById(R.id.tv_start_sign);
            tvLaneEnd = (TextView) itemView.findViewById(R.id.tv_lane_end);
            tvAddChild = (TextView) itemView.findViewById(R.id.tv_add_child);
            tvDeleteChild = (TextView) itemView.findViewById(R.id.tv_delete_child);
        }
    }

    /**
     * 车道点击的接口
     */
    public interface OnLaneClickListener {
        void onAddChild(int position);

        void onDeleteChild(int position);

        void onCollectPoint(int position);

        void onCollectEnd(int position);

        void onStyleChange(int position);
    }

}
